import random
import data

# 总价格
sum_price = 0
# 人数
person_number = 9
# 人均
per_capita = 15
# 临时金额
temp_sum_price = 0
# 荤菜数量
meat_number = 2
# 菜的数量
dish_count = 0
# 菜单
menu = []
# 是否打印
# sout = True
sout = False


def init():
    global sum_price
    # 总价格
    sum_price = 0
    global temp_sum_price
    # 临时金额
    temp_sum_price = 0
    global menu
    # 菜单
    menu = []


def data_input(person_number_in,per_capita_in,meat_in):
    # person_number_input = input("几个人吃(5~10)(9):")
    # per_capita_input = input("预期人均价格(15)：")
    # meat_input = input("想吃多少荤菜呢(2)：")
    person_number_input = person_number_in
    per_capita_input = per_capita_in
    meat_input = meat_in
    if person_number_input == "":
        person_number_input = "9"
    if per_capita_input == "":
        per_capita_input = "15"
    if meat_input == "":
        meat_input = "2"
    if not (person_number_input.isdigit() and per_capita_input.isdigit()):
        print("输入不正确")
    else:
        global person_number
        global per_capita
        global temp_sum_price
        global meat_number
        global sum_price
        person_number = int(person_number_input)
        per_capita = int(per_capita_input) + 1
        temp_sum_price = per_capita * person_number
        meat_number = int(meat_input)


def how_many_dish():
    global person_number
    global dish_count
    if int(person_number) < 8:
        dish_count = person_number
    else:
        dish_count = 8


def choose_soup():
    global menu
    global sum_price
    global temp_sum_price
    global dish_count
    ran = random.randint(0, len(data.soup) - 1)
    soup = data.soup[ran]
    menu.append(soup)
    sum_price += soup["price"]
    temp_sum_price -= soup["price"]
    dish_count -= 1


def choose_rise():
    global menu
    global sum_price
    global temp_sum_price
    global person_number
    name = data.rice[0]['name']
    price = data.rice[0]['price'] * person_number
    dict = {'name': name, 'price': price, 'type': data.rice[0]['type']}
    menu.append(dict)
    sum_price += person_number
    temp_sum_price -= person_number


def choose_dish():
    global dish_count
    global temp_sum_price
    global sum_price
    while dish_count != 0:
        dish_list = data.below(temp_sum_price / dish_count)
        ran = random.randint(0, len(dish_list) - 1)
        dish = dish_list[ran]
        if menu.count(dish) == 0:
            menu.append(dish)
            sum_price += dish["price"]
            temp_sum_price -= dish["price"]
            dish_count -= 1
    return menu


def i_want_to_eat_meat():
    meat = 0
    for i in menu:
        if i["type"] == 1:
            meat += 1
    if meat > meat_number:
        return True
    else:
        return False


def pay():
    result = ""
    if sout:
        print("菜单：")
    result += "菜单：\n"
    for i in menu:
        if sout:
            print(i["name"] + " " + str(i["price"]))
        result += i["name"] + " " + str(i["price"]) + "\n"
    if sout:
        print("总金额：" + str(sum_price))
    result += "总金额：" + str(sum_price) + "\n"
    if sout:
        print("临时总金额：" + str(temp_sum_price))
    # result += "临时总金额：" + str(temp_sum_price) + "\n"
    if sout:
        print("人均约：" + str(round(sum_price / person_number, 2)))
    result += "人均约：" + str(round(sum_price / person_number, 2)) + "\n"
    return result


def what_to_eat(person_number,per_capita,meat):
    temp = 0
    while temp != 1000:
        init()
        data_input(person_number,per_capita,meat)
        how_many_dish()
        choose_soup()
        choose_rise()
        choose_dish()
        if i_want_to_eat_meat():
            break
        temp += 1
    if temp == 1000:
        return "条件太苛刻咯！\n今天中午还是吃白饭吧！"
    else:
        return pay()


if __name__ == "__main__":
    what_to_eat()
